import axios from "axios"
axios.defaults.withCredentials = true
chrome.webRequest.onBeforeSendHeaders.addListener(
    function (details) {
        if(details.url.indexOf("http://jd1988.cn")!=-1){
            details.requestHeaders.push({ name: "X-AjaxPro-Method", value: "GetHistory" })
            return { requestHeaders: details.requestHeaders };
        }
        details.requestHeaders.push({ name: "referer", value: details.url })
        return { requestHeaders: details.requestHeaders };
    },
    {
        urls: ["https://union.jd.com/*","https://cd.jd.com/*","http://jd1988.cn/*","https://item-soa.jd.com/*"]
    },
    ["blocking", "requestHeaders", "extraHeaders"]
);
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    switch (request.type) {
        case "common":
            axios.get(request.url, {
                baseURL: request.baseURL,
                params: request.params
            }).then(res => {
                sendResponse(res)
            })
            return true
        //自定义header
        case "custom_header":
            axios.get(request.url, {
                baseURL: request.baseURL,
                params: request.params
            }).then(res => {
                sendResponse(res.data)
            })
            return true
        case "custom_header_post":
            axios.post(
                request.url,
                request.data, {
                baseURL: request.baseURL,
            }).then(res => {
                sendResponse(res.data)
            })
            return true
        case "custom":
            axios({
                method: 'get',
                url: '/api/get_html',
                baseURL: "http://127.0.0.1:3000",
                withCredentials: true,
                params: {
                    header: request.header,
                    get_type: request.get_type,
                    url: request.url,
                }
            }).then(result => {
                sendResponse(result.data)
            })
            return true

    }


})